﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
public partial class API_A_Passsafe : System.Web.UI.Page
{
    /// <summary>
    /// USERSYSTEM PASSSAFE.API 用户系统API
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        string returnStr = string.Empty;
        string editType = Request.Form["edit"];//操作类型
        switch (editType)
        {
            case "check":
                {
                    returnStr = CheckPassSafe();
                    break;
                }
            case "set":
                {
                    break;
                }
            default: break;
        }
        Response.Write(returnStr);
    }
    /// <summary>
    /// 密保校验
    /// </summary>
    /// <returns></returns>
    protected string CheckPassSafe()
    {
        MODEL.UserPassSafeguard model = new MODEL.UserPassSafeguard();
        BLL.UserPassSafeGuard bll = new BLL.UserPassSafeGuard();
        string returnStr = string.Empty;
        string errNum = string.Empty;
        string codeNum=string.Empty;
        model.UID = GetUserId();
        model.Uanswer = Request.Form["a"];
        model.UquestionID = Convert.ToInt32(Request.Form["q"]);

        if (model.UID != "0")
        {
            bool flag = bll.UserPassSafeGuardCheck(model);
            if (flag)
            {
                errNum = "true";
                Random rdm = new Random();
                codeNum = rdm.Next(0, 999999).ToString();
                Session["codenum"] = codeNum;
                Session["edituser"] = Request.Form["username"];
            }
            else
            {
                errNum = "0";
            }
        }
        else
        {
            errNum = "1";
        }
        returnStr = "{\"key\":\"" + DBUtility.DbHelper.Encrypt(DateTime.Now.ToLongTimeString()) + "\",\"code\":\"" + DBUtility.DbHelper.Encrypt(codeNum) + "\",\"err\":\"" + errNum + "\"}";
        return returnStr;
    }
    /// <summary>
    /// 获取用户编号
    /// </summary>
    /// <returns></returns>
    protected string GetUserId()
    {
        string userName = Request.Form["username"];
        BLL.UserLogon bll = new BLL.UserLogon();
        string userId = string.Empty;
        DataSet ds = bll.UserLogonListByName(userName);
        if (ds.Tables[0].Rows.Count > 0)
        {
            userId = ds.Tables[0].Rows[0]["Uid"].ToString();
        }
        else
        {
            userId = "0";
        }
        return userId;
    }

}
